package com.engineering.elink.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.recruit.CvdBaseInfoCo;
import com.engineering.elink.co.recruit.CvdDataSearchCo;
import com.engineering.elink.co.recruit.CvdDatabaseCo;
import com.engineering.elink.co.recruit.CvdJobIntentionCo;
import com.engineering.elink.co.recruit.CvdSearchCo;
import com.engineering.elink.co.recruit.EducationExperienceAddCo;
import com.engineering.elink.co.recruit.ProjectExperienceAddCo;
import com.engineering.elink.co.recruit.WorkExperienceAddCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.domain.recruit.CardType;
import com.engineering.elink.domain.recruit.CvDatabase;
import com.engineering.elink.domain.recruit.EducationExperience;
import com.engineering.elink.domain.recruit.ProjectExperience;
import com.engineering.elink.domain.recruit.Salary;
import com.engineering.elink.domain.recruit.WorkExperience;
import com.engineering.elink.recruitdao.CardTypeMapper;
import com.engineering.elink.recruitdao.CvDatabaseMapper;
import com.engineering.elink.recruitdao.DictCategoryMapper;
import com.engineering.elink.recruitdao.EducationExperienceMapper;
import com.engineering.elink.recruitdao.FunctionsDetailCategoryMapper;
import com.engineering.elink.recruitdao.ProjectExperienceMapper;
import com.engineering.elink.recruitdao.SalaryMapper;
import com.engineering.elink.recruitdao.VitaCollectionMapper;
import com.engineering.elink.recruitdao.VitaDeliveryMapper;
import com.engineering.elink.recruitdao.WorkExperienceMapper;
import com.engineering.elink.service.CvDatabaseService;
import com.engineering.elink.util.ResJsonUtil;
import com.engineering.elink.videodao.UserViewMapper;
import com.engineering.elink.vo.recruit.CvDataBaseVo;
import com.engineering.elink.vo.recruit.CvdUserVo;
import com.engineering.elink.vo.recruit.JobIntentionVo;
import com.engineering.elink.vo.recruit.SalaryVo;
import com.engineering.elink.xaservice.CvdDatabaseXaService;

/**
 * 简历业务实现类
 */
@Service
public class CvDatabaseServiceImpl implements CvDatabaseService{
	
	private static Logger logger = Logger.getLogger(CvDatabaseServiceImpl.class);

	@Autowired
	private CardTypeMapper cardTypeMapper;
	
	@Autowired
	private CvDatabaseMapper cvDatabaseMapper;
	
	@Autowired
	private CvdDatabaseXaService cvdDatabaseXaService;
	
	@Autowired
	private DictCategoryMapper dictCategoryMapper;
	
	@Autowired
	private FunctionsDetailCategoryMapper functionsDetailCategoryMapper;
	
	@Autowired
	private UserViewMapper userViewMapper;
	
	@Autowired
	private SalaryMapper salaryMapper;
	
	@Autowired
	private VitaDeliveryMapper vitaDeliveryMapper;
	
	@Autowired
	private VitaCollectionMapper vitaCollectionMapper;
	
	@Autowired
	private WorkExperienceMapper workExperienceMapper;
	
	@Autowired
	private EducationExperienceMapper educationExperienceMapper;
	
	@Autowired
	private ProjectExperienceMapper projectExperienceMapper;
	
	@Override
	public List<CardType> getCardType() {
		
		return cardTypeMapper.selectAll();
	}

	@Override
	public Map<String, Object> updateOpen(String cvdId,Integer open) {
		
		CvDatabase cvd=cvDatabaseMapper.selectByPrimaryKey(cvdId);
		if(cvd==null)
			return ResJsonUtil.getMap(ErrorType.CVDATABASE_ERROR);
		cvd.setOpen(open);
		cvd.setUpdateTime(new Date());
		cvDatabaseMapper.updateByPrimaryKeySelective(cvd);
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> saveJobIntention(String cvdId, CvdJobIntentionCo jobIntentionCo, String userId) {
		logger.info("保存简历求职意向开始！！！");
		try {
			cvdDatabaseXaService.saveJobIntention(cvdId,jobIntentionCo, userId);
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDJOBINTENTION_ADD_ERROR,e.getMessage());
		} 
		
	}

	@Override
	public Map<String, Object> saveBaseInfo(String cvdId, CvdBaseInfoCo baseInfoCo, String userId) {
		logger.info("保存简历基本信息开始！！！");
		try {
			cvdDatabaseXaService.saveBaseInfo(cvdId,baseInfoCo, userId);
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDBASEINFO_ADD_ERROR,e.getMessage());
		} 
	}

	@Override
	public Map<String, Object> saveWorkExperience(String cvdId, String jsonStr, String userId) {
		logger.info("批量保存工作经验开始！！！");
		try {
			cvdDatabaseXaService.saveWorkExperience(cvdId,jsonStr, userId);
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDWORKEXPERIENCE_ADD_ERROR,e.getMessage());
		}
	}

	@Override
	public Map<String, Object> saveEducationExperience(String cvdId, String jsonStr, String userId) {
		logger.info("批量保存教育经历开始！！！");
		try {
			cvdDatabaseXaService.saveEducationExperience(cvdId,jsonStr, userId);
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDEDUEXPERIENCE_ADD_ERROR,e.getMessage());
		}
	}

	@Override
	public Map<String, Object> saveProjectExperience(String cvdId, String jsonStr, String userId) {
		logger.info("批量保存项目经验开始！！！");
		try {
			cvdDatabaseXaService.saveProjectExperience(cvdId,jsonStr, userId);
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDPROJECTEXPERIENCE_ADD_ERROR,e.getMessage());
		}
	}

	@Override
	public Map<String, Object> get(String userId,String cvdId) {
		
		if(StringUtils.isBlank(userId) && StringUtils.isBlank(cvdId)){
			return ResJsonUtil.getMap(ErrorType.GET_CVDUSER_ERROR);
		}
		int count=cvDatabaseMapper.countCvdataByUserId(userId);
		if(count==0){
			count=cvDatabaseMapper.countCvdataByCvdId(cvdId);
		}
		CvDataBaseVo cvdVo=null;
		if(count>0){
			cvdVo=cvDatabaseMapper.selectInfo(userId,cvdId);
			if(StringUtils.isNotEmpty(cvdVo.getFunctionsIds())){
				cvdVo.setCvdFdcList(functionsDetailCategoryMapper.selectBatchByFdcIds(StringUtils.split(cvdVo.getFunctionsIds(),',')));
			}
			if(StringUtils.isNotEmpty(cvdVo.getGuildIds())){
				cvdVo.setCvdGuildList(this.dictCategoryMapper.selectBatchByGuildIds(StringUtils.split(cvdVo.getGuildIds(), ',')));
			}
			
			JobIntentionVo jiVo=new JobIntentionVo();
			jiVo.setArrivalTime(cvdVo.getArrivalTime());
			jiVo.setCvdEwpAddressList(cvdVo.getCvdEwpAddressList());
			jiVo.setCvdFdcList(cvdVo.getCvdFdcList());
			jiVo.setCvdGuildList(cvdVo.getCvdGuildList());
			jiVo.setDc(cvdVo.getDc());
			jiVo.setFunctionsIds(cvdVo.getFunctionsIds());
			jiVo.setGuildIds(cvdVo.getGuildIds());
			jiVo.setNatureId(cvdVo.getNatureId());
			jiVo.setSalaryId(cvdVo.getSalaryId());
			jiVo.setSalaryObject(cvdVo.getSalaryObject());
			
			cvdVo.setJobIntentionVo(jiVo);
			
		}else{
			cvdVo=new CvDataBaseVo();
			//从用户取数据初始化手机和邮箱去前台
			CvdUserVo vo=userViewMapper.selectCvdUser(userId);
			if(vo!=null){
				cvdVo.setEmail(vo.getEmail());
				cvdVo.setPhone(vo.getPhone());
			}
		}
		return ResJsonUtil.getMap(ErrorType.SUCCESS, cvdVo);
		
	}

	@Override
	public Map<String, Object> saveCvdDatabase(String cvdId, CvdBaseInfoCo baseInfoCo, CvdJobIntentionCo jobIntentionCo,
			String workJsonStr, String eduJsonStr, String projectJsonStr, String userId) {
		logger.info("保存简历所有信息开始！！！");
		try {
			CvdDatabaseCo co=new CvdDatabaseCo();
			co.setCvdId(cvdId);
			co.setBaseInfoCo(baseInfoCo);
			co.setEduJsonStr(eduJsonStr);
			co.setJobIntentionCo(jobIntentionCo);
			co.setProjectJsonStr(projectJsonStr);
			co.setWorkJsonStr(workJsonStr);
			
			cvdDatabaseXaService.saveCvdDatabase(co,userId);
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
			
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVD_ALL_ADD_ERROR,e.getMessage());
			
		}
	}
	
	@Override
	public List<Salary> getSalary() {
		
		return this.salaryMapper.selectAll();
	}

	@Override
	public List<SalaryVo> getSalaryByLimit(Integer count) {
		
		return this.salaryMapper.selectSalaryByLimit(count);
	}

	@Override
	public Map<String, Object> getCvdDeliveryList(CvdSearchCo co, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", cvDatabaseMapper.selectCvdDeliveryList(co,userId));
		map.put("Pagination", new Pagination(co, cvDatabaseMapper.countCvdDelivery(co,userId)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> batchMoveCvd(String vdIds, String userId) {

		if(StringUtils.isEmpty(vdIds)){
			return ResJsonUtil.getMap(ErrorType.REQUEST_PARAMS_ERROR);
		}
		logger.info("批量移至简历库开始！！！");
		try {
			cvdDatabaseXaService.batchMoveCvd(vdIds, userId);
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
			
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.BATCH_CVD_ERROR,e.getMessage());
			
		}
		
	}
	
	@Override
	public Map<String, Object> getCvdCollectionList(CvdSearchCo co, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", cvDatabaseMapper.selectCvdCollectionList(co,userId));
		map.put("Pagination", new Pagination(co, cvDatabaseMapper.countCvdCollection(co,userId)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> batchUpdateCvdStatus(String vcIds,Integer status,String userId) {
		if(StringUtils.isEmpty(vcIds) || status==null){
			return ResJsonUtil.getMap(ErrorType.REQUEST_PARAMS_ERROR);
		}
		this.vitaCollectionMapper.updateBatchByStatus(status, StringUtils.split(vcIds,','));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> saveWorkExp(String cvdId, WorkExperienceAddCo workCo, String userId) {
		logger.info("保存工作经验开始！！！");
		try {
			cvdDatabaseXaService.saveWorkExp(cvdId,workCo, userId);
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDWORKEXPERIENCE_ADD_ERROR,e.getMessage());
			
		}
	}
	
	@Override
	public Map<String, Object> updateWorkExp(String cvdId,Integer weId, WorkExperienceAddCo workCo, String userId) {
		logger.info("更新工作经验开始！！！");
		try {
			cvdDatabaseXaService.updateWorkExp(cvdId,weId,workCo, userId);
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDWORKEXPERIENCE_UPDATE_ERROR,e.getMessage());
			
		}
	}

	@Override
	public Map<String, Object> deleteWorkExp(Integer weId, String userId) {
		WorkExperience we=workExperienceMapper.selectByPrimaryKey(weId);
		if(we!=null){
			workExperienceMapper.deleteByPrimaryKey(weId);
		}
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> saveEduExp(String cvdId, EducationExperienceAddCo eeCo, String userId) {
		logger.info("保存教育经验开始！！！");
		try {
			cvdDatabaseXaService.saveEduExp(cvdId,eeCo, userId);
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDEDUEXPERIENCE_ADD_ERROR,e.getMessage());
		}
	}

	@Override
	public Map<String, Object> updateEduExp(String cvdId, Integer eeId, EducationExperienceAddCo eeCo, String userId) {
		logger.info("更新教育经历开始！！！");
		try {
			cvdDatabaseXaService.updateEduExp(cvdId,eeId,eeCo,userId);
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
			
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDEDUEXP_UPDATE_ERROR,e.getMessage());
			
		}
	}

	@Override
	public Map<String, Object> deleteEduExp(Integer eeId, String userId) {
		EducationExperience ee=educationExperienceMapper.selectByPrimaryKey(eeId);
		if(ee!=null){
			educationExperienceMapper.deleteByPrimaryKey(eeId);
		}
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> saveProjectExp(String cvdId, ProjectExperienceAddCo peCo, String userId) {
		logger.info("保存项目经验开始！！！");
		try {
			
			cvdDatabaseXaService.saveProjectExp(cvdId,peCo, userId);
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
			
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDPROJECTEXPERIENCE_ADD_ERROR,e.getMessage());
			
		}
	}

	@Override
	public Map<String, Object> updateProjectExp(String cvdId, Integer peId, ProjectExperienceAddCo peCo, String userId) {
		logger.info("更新项目经验开始！！！");
		try {
			
			cvdDatabaseXaService.updateProjectExp(cvdId,peId,peCo,userId);
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
			
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.CVDPROJECTEXP_UPDATE_ERROR,e.getMessage());
			
		}
	}
	
	@Override
	public Map<String, Object> deleteProExp(Integer peId, String userId) {
		ProjectExperience pe=projectExperienceMapper.selectByPrimaryKey(peId);
		if(pe!=null){
			projectExperienceMapper.deleteByPrimaryKey(peId);
		}
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> getSearchCvdList(CvdDataSearchCo co, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", cvDatabaseMapper.selectSearchCvdList(co,userId));
		map.put("Pagination", new Pagination(co, cvDatabaseMapper.countSearchCvdList(co)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> batchUpdateCvdType(String vdIds, Integer type, String userId) {
		if(StringUtils.isEmpty(vdIds) || type==null){
			return ResJsonUtil.getMap(ErrorType.REQUEST_PARAMS_ERROR);
		}
		this.vitaDeliveryMapper.updateBatchByType(type, StringUtils.split(vdIds,','));
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

}
